package com.project.fiveCerealsSavoury.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.omg.PortableInterceptor.USER_EXCEPTION;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.fiveCerealsSavoury.dao.UserDao;
import com.project.fiveCerealsSavoury.domain.LogitsticsState;
import com.project.fiveCerealsSavoury.domain.Order;
import com.project.fiveCerealsSavoury.domain.OrderState;
import com.project.fiveCerealsSavoury.domain.User;
import com.project.fiveCerealsSavoury.exception.BizException;
import com.project.fiveCerealsSavoury.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userdao;
	/**
	 * 注册
	 */
	@Override
	public void reg(User user) throws BizException {
		User user2 = new User();
		user2.setName(user.getName());
		user2.setPasswd(user.getPasswd());
		user2.setPhone(user.getPhone());
		user2.setTime(new Timestamp(new Date().getTime()));
		user2.setAdmin(user.isAdmin());
		List<User> queryName = queryName(user);
		for (User user3 : queryName) {
			if(user2.getName().equals(user3.getName())){
				throw new BizException("用户已存在");
			}
			if(user2.getPhone().equals(user3.getPhone())){
				throw new BizException("电话号码已被注册");
			}
		}
		userdao.insert(user2);
	}
	/**
	 * 登录
	 */
	@Override
	public User login(String name, String passwd) throws BizException{
		User query = userdao.query(name, passwd);
		
		if(query == null) throw new BizException("用户不存在");
		return query;
	}

	@Override
	public int setUser(String newPasswd,String newPhone,Integer userId) throws BizException {
		int set = userdao.set(newPasswd,newPhone,userId);
		if(set == 0) throw new BizException("用户不存在");
		return set;
	}

	@Override
	public int fullMoney(Double money,Integer id) throws BizException{
		 int addMoney = userdao.addMoney(money,id);
		 if(addMoney == 0) throw new BizException("充值失败");
		return addMoney;
	}

	@Override
	public User query(Integer userId) throws BizException {
		User queryName = userdao.queryId(userId);
		if(queryName ==null)throw new BizException("用户不存在");
		return queryName;
	}
	@Override
	public List<User> queryName(User user) {
		List<User> queryName = userdao.queryName(user);
		for (User user2 : queryName) {
			if(user2 !=null){
				return queryName;
			}
		}
		return null;
	}
	/**
	 * 查询所有管理员
	 */
	@Override
	public List<User> queryIsAdmin(String isAdmin) throws BizException{
		if(isAdmin == null)throw new BizException("不能为空");
		List<User> queryAdmin = userdao.queryIsAdmin(isAdmin);
		if(queryAdmin == null)throw new BizException("管理员不存在");
			return queryAdmin;
	}
//	@Override
//	public boolean remove(Integer id) throws BizException {
//		if(id == null) throw new BizException("用户不存在");
//		User queryId = userdao.queryId(id);
//		if(queryId !=null){
//			userdao.remove(id);
//			return true;
//		}
//		return false;
//	}
	/**
	 * 修改是否签收
	 */
	@Override
	public boolean remend(Integer id, String isSign) throws BizException{
		if(id == null)throw new BizException("用户不存在");
		if(isSign == null)throw new BizException("无是否签收");
		if(LogitsticsState.已送达 != null){
			return userdao.remend(id, isSign);
		}
		return false;
	}
}
